package com.winter.service.impl;

import com.winter.mapper.TopicMapper;
import com.winter.mapper.UserMapper;
import com.winter.model.Topic;
import com.winter.model.User;
import com.winter.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Service
public class TopicServiceImpl implements TopicService {
	@Autowired
	private TopicMapper mapper;
	@Autowired
	private UserMapper userMapper;
	/*
	 *
	 *
	 * 提问service
	 *
	 * (non-Javadoc)
	 * @see com.winter.service.TopicService#askQuestion(java.util.Map)
	 */
	@Override
	@Transactional
	public void askQuestion(Map<String, Object> paramMap) {
		long tid =paramMap.containsKey("tid")?(long)paramMap.get("tid"):0;
		if(tid==0){//新增
			Long userId =(long) 1;//暂时使用
			String createUser ="ball";
			SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd HHmmss");
			Topic topic =new Topic();
			topic.setNumber(paramMap.containsKey("number")?(String)paramMap.get("number"):null);//主帖编号
			topic.setSid(paramMap.containsKey("sid")?(long)paramMap.get("sid"):null);//板块id
			topic.setUserId(userId);//提问者id
			topic.setTopic(paramMap.containsKey("topic")?(String)paramMap.get("topic"):null);//标题
			topic.setContente(paramMap.containsKey("contente")?(String)paramMap.get("contente"):null);//主帖内容
			topic.setFlag(1);//新建帖子默认状态是无人回答
			topic.setLastClickt(new Date());//新建帖子默认最后点击时间为当前时间
			topic.setDelState(1);//默认未删除
			topic.setCreateDate(new Date());
			topic.setCreateUser(createUser);
			topic.setUpdateDate(new Date());
			topic.setUpdateUser(createUser);
			topic.setTime(new Date());
			topic.setFee(paramMap.containsKey("fee")?(BigDecimal)paramMap.get("fee"):null);//赏金
			topic.setTopicFlag(paramMap.containsKey("topicflag")?(int)paramMap.get("topicflag"):null);//帖子发布状态
			mapper.insert(topic);
		}else{
			Long userId =(long) 1;//暂时使用
			String createUser ="ball";
			SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd HHmmss");
			Topic topic =mapper.selectByPrimaryKey(tid);
			topic.setNumber(paramMap.containsKey("number")?(String)paramMap.get("number"):null);//主帖编号
			topic.setSid(paramMap.containsKey("sid")?(long)paramMap.get("sid"):null);//板块id
			topic.setUserId(userId);//提问者id
			topic.setTopic(paramMap.containsKey("topic")?(String)paramMap.get("topic"):null);//标题
			topic.setContente(paramMap.containsKey("contente")?(String)paramMap.get("contente"):null);//主帖内容
			topic.setFlag(1);//新建帖子默认状态是无人回答
			topic.setLastClickt(new Date());//新建帖子默认最后点击时间为当前时间
			topic.setDelState(1);//默认未删除
			topic.setCreateDate(new Date());
			topic.setCreateUser(createUser);
			topic.setUpdateDate(new Date());
			topic.setUpdateUser(createUser);
			topic.setTime(new Date());
			topic.setFee(paramMap.containsKey("fee")?(BigDecimal)paramMap.get("fee"):null);//赏金
			topic.setTopicFlag(paramMap.containsKey("topicflag")?(int)paramMap.get("topicflag"):null);//帖子发布状态
			mapper.updateByPrimaryKeySelective(topic);


		}


	}

	//如果帖子已发布不能编辑
	@Override
	public String checkEdit(long tid) {
		Topic topic =mapper.selectByPrimaryKey(tid);
		int topicFlag=topic.getTopicFlag();
		if(topicFlag==1){//草稿
			return "false";

		}else{//已发布
			return "true";
		}

	}

	//用户设置赏金时校验
	@Override
	public String checkFee(Map<String, Object> paramMap) {
		User user =userMapper.selectByPrimaryKey((long)paramMap.get("userId"));
		BigDecimal money =user.getMoney();
		BigDecimal orderFee =paramMap.containsKey("fee")?((BigDecimal)paramMap.get("fee")):null;
		if(money.compareTo(orderFee)<0){
			return "true";

		}else{
			return "false";
		}

	}

	//校验提问者设置时间的长度
	public String checkSettingTime(Map<String, Object> paramMap){

		//暂时不判断是否有中期
		Date endDate= (Date)paramMap.get("endDate");//提问者设置的时间
	    int day =7;//默认至少一个星期的答题时间只多不少

		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");

		calendar.add(Calendar.DATE, day);
		Date date =calendar.getTime();
		if(date.after(endDate)){//如果规定时间大于提问者设置的时间
           return "true";
		}else{
          return "false";
		}

	}

	
}
